/**
 * @file app\motor\motor.c
 *
 * Copyright (C) 2023
 *
 * dp_key.c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author zhuodada
 *
 * @encoding utf-8
 */

/*---------- includes ----------*/
#include "device.h"
#include "gpio_group.h"
#include "protocol_mobus.h"
#include "resources.h"
#include "soft_timer.h"
#include "storage.h"

/*---------- macro ----------*/
#define TAG "MOTOR"

/*---------- type define ----------*/
/*---------- variable prototype ----------*/
/*---------- function prototype ----------*/
/*---------- variable ----------*/
static void* dev_motor;
static uint16_t value = 0;
uint8_t count_ = 2;;
/*---------- function  ----------*/
static void _timer_expired(timer_handle_t timer) {
    if (count_ > 0)
        count_--;
    else
        device_ioctl(dev_motor, IOCTL_GROUP_SET, &value);
}
void app_motor_poll(void) {
    void* proc_timer;

    dev_motor = resources_get("dev_motor");
    assert(dev_motor);

    /* create a timer for proc polling */
    proc_timer = soft_timer_create("MotorProcTimer", SFTIM_MODE_REPEAT,
        __ms2ticks(100), NULL, _timer_expired);
    assert(proc_timer);
    soft_timer_start(proc_timer);
    /*say hi */
    xlog_tag_message(TAG, "Initialize successfully\n");
}
